import Axios from 'axios'
import { invokeAsyncAPi } from '@/xz-fn.js'
import { isPassVersionByApp } from '@/utils/judge'
// import { invokeAPi } from '@/xz-fn.js'
import { getBackEndConfig } from '@/api/common'
const state = {
  info: {
    needProtocol: false,
    merchant: '', // 商户id
    semShortId: '', // sem上报短id
    appID: '', // 客户端id
    app_name: '', // 应用名称
    installApp: [],
    channel: '', // 渠道信息
    uuid: '', // 设备信息
    device_name: '', // 设备名称
    version: '', // 版本号
    pubkey: '',
    allPackages: [] // 在售权益包
  },
  vipGuide: {
    show: false,
    eid: '',
    peid: '',
    title: ''
  },
  isDependeceAiVersion: false,

  btnCfg: {
    vip: {
      title: '升级会员',
      content: ''
    },
    jgb: {
      title: '购买能量包',
      content: ''
    }
  }
}

const mutations = {
  SET_DEVICEINFO (state, deviceInfo) {
    // deviceInfo.installApp = '10003'
    // deviceInfo.unionid = '4403'
    // deviceInfo.version = '3.2.10.8'
    state.info.appID = deviceInfo.appID
    state.info.app_name = deviceInfo.app_name
    state.info.installApp = deviceInfo.installApp ? String(deviceInfo.installApp).split(',') : ['10001', '10003', '10004']
    state.info.channel = deviceInfo.unionid
    state.info.uuid = deviceInfo.pcid
    state.info.device_name = deviceInfo.device_name
    state.info.device_name = deviceInfo.device_name
    state.info.version = deviceInfo.version || '3.0.0.32'
    state.isDependeceAiVersion = process.env.VUE_APP_NAME === 'xz' ? isPassVersionByApp({
      '22': { v: '3.3.1.23' },
      '111': { v: '3.2.10.0' }
    }) : true
    // state.isDependeceAiVersion = true
  },
  SET_VIPGUIDE (state, param) {
    state.vipGuide = param
  },
  SET_BTNCFG (state, param) {
    state.btnCfg = Object.assign({}, state.btnCfg, param)
  }
}

const actions = {
  getDeviceInfo ({ commit, dispatch }) {
    return new Promise((resolve, reject) => {
      // 本地调用
      // eslint-disable
      // const res1 = {
      //   "appID": 22,
      //   "device_name": "DESKTOP-NVA31LA",
      //   "installApp": 22,
      //   "pcid": "c93e43453d5733fac135db0271437b35",
      //   "unionid": 4003,
      //   "version": "4.0.0.1"
      // }
      // commit('SET_DEVICEINFO', res1)
      // resolve()

      const name = process.env.VUE_APP_XZ_GET_DEVICE_INFO
      Axios.all([invokeAsyncAPi(name)]).then(Axios.spread(function (res1) {
        commit('SET_DEVICEINFO', res1)
        resolve()
      })).catch(error => {
        reject(error)
      })
    })
  },

  setVipGuide ({ commit, dispatch }, { status, title, eid, peid }) {
    commit('SET_VIPGUIDE', {
      show: status,
      title: title,
      eid: String(eid) || '',
      peid: String(peid) || ''
    })
  },

  async getBtnCfg ({ commit, state }) {
    const { data } = await getBackEndConfig({
      app_id: state.info.appID,
      version: state.info.version,
      union_id: state.info.channel,
      keyname: 'gmanpz'
    })
    const cfg = {}
    if (data?.params?.vip_btn) {
      cfg['vip'] = data.params.vip_btn
    }
    if (data?.params?.jgb_btn) {
      cfg['jgb'] = data.params.jgb_btn
    }
    Object.keys(cfg).length && commit('SET_BTNCFG', cfg)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
